package cn.yangliu.operation.config;

import cn.yangliu.comm.tools.SpringUtil;
import cn.yangliu.operation.aspect.OperationAspect;
import cn.yangliu.operation.entity.Operation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 作者 杨柳
 * 创建时间 2017-09-07 20:16
 */
@Configuration
@EnableConfigurationProperties(value = OperationProperties.class)
@ConditionalOnProperty(prefix = "spring.operate", value = "enable", matchIfMissing = true)
public class OperationAutoConfiguration {

    @Resource
    private OperationProperties properties;

    @Bean
    public OperationAspect operationAspect() {
        OperationAspect aspect = new OperationAspect();
        aspect.setOrder(properties.getAopOrder());
        return aspect;
    }

    @Bean
    @ConditionalOnMissingBean(SpringUtil.class)
    public SpringUtil springUtil() {
        return new SpringUtil();
    }

    @Bean
    public OperationConfig operationConfig() {
        OperationConfig config = new OperationConfig();
        Class<? extends Operation> clazz = properties.getEntityClass();
        if (clazz!=null){
            config.setEntityClassName(properties.getEntityClass());
        }
        return config;
    }
}
